
package com.xifan.myaccount.widget;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.xifan.myaccount.R;

import java.text.DecimalFormat;

public class CalcListener implements OnClickListener {

    private static final int ADD = 1;
    private static final int SUB = 2;
    private static final int MUL = 3;
    private static final int DIV = 4;

    private MoneyView mNum1View;
    private TextView mNum2View;
    private TextView mOpView;
    private DecimalFormat df;
    private int op;
    private double result;

    private double num1;
    private double num2 = 0;
    private boolean hasDot = false;

    public CalcListener(MoneyView num1view, TextView num2view, TextView opview) {
        mNum1View = num1view;
        mNum2View = num2view;
        mOpView = opview;
        df = new DecimalFormat("##0.00");
    }

    private void doEqual(boolean multiOp) {
        switch (op) {
            case ADD:
                result = num1 + num2;
                break;
            case SUB:
                result = num1 - num2;
                break;
            case MUL:
                result = num1 * num2;
                break;
            case DIV:
                result = num1 / num2;
                break;
            default:
                num1 = Double.parseDouble(mNum1View.getText());
                if (num1 != 0d) {
                    result = num1;
                } else {
                    result = num1 = 0d;
                }
                break;
        }
        mNum1View.setText(String.valueOf(result));
        if (multiOp) {
            num2 = 0;
            mNum2View.setText("0.00");
            hasDot = false;
        } else {
            clear(true);
        }
    }

    private void clear(boolean isEqual) {
        if (!isEqual) {
            mNum1View.setText(null);
        }
        num1 = num2 = 0;
        op = 0;
        result = 0;
        mOpView.setText(null);
        mNum2View.setText("0.00");
        hasDot = false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.equal:
                doEqual(false);
                break;
            case R.id.clear:
                clear(false);
                break;
            case R.id.add:
                if (num2 != 0)
                    doEqual(true);

                op = ADD;
                mOpView.setText("+");
                num1 = Double.parseDouble(mNum1View.getText());
                hasDot = false;
                break;
            case R.id.sub:
                if (num2 != 0)
                    doEqual(true);

                op = SUB;
                mOpView.setText("-");
                num1 = Double.parseDouble(mNum1View.getText());
                hasDot = false;
                break;
            case R.id.mul:
                if (num2 != 0)
                    doEqual(true);

                op = MUL;
                mOpView.setText("×");
                num1 = Double.parseDouble(mNum1View.getText());
                hasDot = false;
                break;
            case R.id.div:
                if (num2 != 0)
                    doEqual(true);

                op = DIV;
                mOpView.setText("÷");
                num1 = Double.parseDouble(mNum1View.getText());
                hasDot = false;
                break;
            case R.id.dot:
                hasDot = true;
                break;
            default:
                // Typing number
                Double moneyText = Double.parseDouble(((Button) v).getText().toString());
                if (op == 0) { // First input
                    if (hasDot) {
                        // 如果num1已有小数，则在百分位上相加，否则在十分位上相加
                        if (num1 > (int) num1) {
                            num1 = num1 + moneyText / 100d;
                        } else {
                            num1 = num1 + moneyText / 10d;
                        }
                    } else {
                        if (num1 == 0) { // no number input and no dot
                            num1 = moneyText;
                        } else {
                            num1 = num1 * 10d + moneyText;
                        }
                    }
                    mNum1View.setText(String.valueOf(num1));
                    mNum2View.setText(df.format(num1));
                } else {
                    if (hasDot) {
                        if (num2 > (int) num2) {
                            num2 = num2 + moneyText / 100d;
                        } else {
                            num2 = num2 + moneyText / 10d;
                        }
                    } else {
                        if (num2 == 0) {
                            num2 = moneyText;
                        } else {
                            num2 = num2 * 10d + moneyText;
                        }
                    }
                    mNum2View.setText(df.format(num2));
                }
        }
    }
}
